//  [5/4/2013 OMEGA] created

#include "DuelREditorCommon.h"
#include "DuelREDescriptoinFileWindow.h"
#include "DuelREDescriptionFileEditDelegate.h"
#include "DuelRProject.h"

namespace DuelResourceEditor
{
#define DESCFILETABLE_HEADER_INDEX	0
#define DESCFILETABLE_HEADER_TYPE	1
#define DESCFILETABLE_HEADER_NAME	2


	REDescriptionFileModel::REDescriptionFileModel( QObject* parent /*= 0*/ ) :
		QAbstractTableModel(parent),
		mTargetProj(NULL)
	{

	}

	REDescriptionFileModel::~REDescriptionFileModel()
	{

	}

	void REDescriptionFileModel::loadFromProject( RProject* proj, REDescriptionFileEditDelegate* editDelegate )
	{
		mTargetProj = proj;
		mEditDelegate = editDelegate;
		update();
	}

	void REDescriptionFileModel::unload()
	{
		mTargetProj = NULL;
		mEditDelegate = NULL;
		update();
	}

	void REDescriptionFileModel::update()
	{
		if (mTargetProj)
		{
			mDescs.clear();
			RProject::DescriptionFIleIterator ai = mTargetProj->getDescriptionFileIterator();
			while(ai.hasMoreElements())
			{
				RDescriptionFile* descf = ai.getNext();
				mDescs.push_back(QPair<QString,QString>(
					descf->getType().c_str(), 
					descf->getOutputFile().c_str()));
			}
		}
		else
		{
			mDescs.clear();
		}
		reset();
	}

	int REDescriptionFileModel::rowCount( const QModelIndex &parent /*= QModelIndex( ) */ ) const
	{
		if (mTargetProj)
		{
			return mTargetProj->getArchiveCount();
		}
		return 0;
	}

	int REDescriptionFileModel::columnCount( const QModelIndex &parent /*= QModelIndex( ) */ ) const
	{
		return 3;
	}

	QVariant REDescriptionFileModel::data( const QModelIndex &index, int role /*= Qt::DisplayRole */ ) const
	{
		if (mTargetProj == NULL)
		{
			return QVariant();
		}
		if (!index.isValid())
			return QVariant();

		if (index.row() >= mDescs.count() || index.row() < 0)
			return QVariant();

		QPair<QString, QString> itm = mDescs.value(index.row());
		if (role == Qt::DisplayRole) {
			if (index.column() == DESCFILETABLE_HEADER_INDEX)
			{
				return index.row();
			}
			else if (index.column() == DESCFILETABLE_HEADER_TYPE)
			{
				return itm.first;
			}
			else if (index.column() == DESCFILETABLE_HEADER_NAME)
			{
				return itm.second;
			}
		}

		return QVariant();
	}

	QVariant REDescriptionFileModel::headerData( int section, Qt::Orientation orientation, int role ) const
	{
		if (role != Qt::DisplayRole)
			return QVariant();

		if (orientation == Qt::Horizontal) {
			switch (section) {
			case DESCFILETABLE_HEADER_INDEX:
				return tr("");
			case DESCFILETABLE_HEADER_TYPE:
				return tr("Type");
			case DESCFILETABLE_HEADER_NAME:
				return tr("FileName");
			default:
				return QVariant();
			}
		}
		return QVariant();
	}


	REDescriptionFileWindow::REDescriptionFileWindow( QWidget* parent ) :
		QDockWidget(tr("DescriptionFiles"), parent)
	{

		mContainer = new QWidget(this);
		setWidget(mContainer);

		mLayout = new QVBoxLayout(mContainer); 
		mLayout->setObjectName(QString::fromUtf8("verticalLayout"));
		mLayout->setContentsMargins(2, 2, 2, 2);

		mContainer->setLayout(mLayout);

		mView = new QTableView(mContainer);
		mView->setObjectName(tr("ResourcePoolView"));
		mView->setAlternatingRowColors(true);
		mView->setSelectionBehavior(QAbstractItemView::SelectRows);
		mView->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
		mView->setShowGrid(false);

		mLayout->addWidget(mView);

		mModel = new REDescriptionFileModel(this);
		mView->setModel(mModel);
		mView->horizontalHeader()->setResizeMode(DESCFILETABLE_HEADER_INDEX, QHeaderView::ResizeToContents);
		mView->horizontalHeader()->setResizeMode(DESCFILETABLE_HEADER_TYPE, QHeaderView::ResizeToContents);
		mView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);

	}

	REDescriptionFileWindow::~REDescriptionFileWindow()
	{

	}

	void REDescriptionFileWindow::loadFromProject( RProject* proj, REDescriptionFileEditDelegate* editDelegate )
	{
		assert(mModel != NULL);
		mModel->loadFromProject(proj, editDelegate);
		editDelegate->setTargetEditWindow(this);
	}

	void REDescriptionFileWindow::unload()
	{
		assert(mModel != NULL);
		mModel->unload();
	}

	void REDescriptionFileWindow::update()
	{
		assert(mModel != NULL);
		mModel->update();

	}

}